package com.cn.zpark.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackageName: com.cn.zpark.filter
 * @ClassName: EncodingFilter
 * @Description:过滤器，过滤乱码问题
 * @author: RZS
 * @date: 2022/4/29  10:41
 */
@WebFilter("*.do")
public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 强转请求对象与响应对象
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        // 解决请求乱码与响应乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        // 放行
        chain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }
}
